﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;

public partial class Media_Media : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ApplyAuthorizationRules();
        }
    }

    private void ApplyAuthorizationRules()
    {
        Media obj = GetMedia();
        if (Media.CanEditObject())
        {
            if (obj.IsNew)
                this.MediaDetailsView.DefaultMode = DetailsViewMode.Insert;
            else
                this.MediaDetailsView.DefaultMode = DetailsViewMode.Edit;
        }
        else
        {
            this.MediaDetailsView.DefaultMode = DetailsViewMode.ReadOnly;
            if (Utils.IsMediaEmployee || Utils.IsMediaManager || Utils.IsGeneralManager
                || Utils.IsFinanceEmployee)
            {
                this.MediaDetailsView.Rows[this.MediaDetailsView.Rows.Count - 2].Visible =
                this.MediaDetailsView.Rows[this.MediaDetailsView.Rows.Count - 3].Visible = true;
            }
            else
            {
                this.MediaDetailsView.Rows[this.MediaDetailsView.Rows.Count - 2].Visible =
                this.MediaDetailsView.Rows[this.MediaDetailsView.Rows.Count - 3].Visible = false;
            }
        }

        if (!obj.IsNew)
        {
            this.MediaDetailsView.Rows[6].Visible = obj.Category == MediaType.TV;
        }

        //ContactListControl1.Editable = Media.CanEditObject();
    }

    #region BusinessObject

    private Media GetMedia()
    {
        string idString = Request.QueryString["id"];
        object businessObj = Session["CurrentObj"];
        if (businessObj == null
            || !(businessObj is Media)
            || (!IsPostBack && !((Media)businessObj).Id.ToString().Equals(idString)))
        {
            try
            {
                if (!string.IsNullOrEmpty(idString))
                {
                    businessObj = Media.GetMedia(int.Parse(idString));
                }
                else
                {
                    businessObj = Media.NewMedia();
                }
                Session["CurrentObj"] = businessObj;
            }
            catch (System.Security.SecurityException)
            {
                Response.Redirect("~/Media");
            }
        }
        return businessObj as Media;
    }

    private int SaveMedia(Media media)
    {
        int rowsAffected = 0;
        try
        {
            Session["CurrentObj"] = media.Save();
            rowsAffected = 1;
        }
        catch (Csla.Validation.ValidationException ex)
        {
            throw;
        }
        catch (Csla.DataPortalException ex)
        {
            throw;
        }
        catch (Exception ex)
        {
            throw;
        }
        return rowsAffected;
    }

    #endregion

    #region MediaDataSource

    protected void MediaDataSource_DeleteObject(object sender, Csla.Web.DeleteObjectArgs e)
    {
        try
        {
            Media.DeleteMedia(int.Parse(e.Keys["Id"].ToString()));
            Session["currentObj"] = null;
            e.RowsAffected = 1;
        }
        catch (Csla.DataPortalException ex)
        {
            throw;
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    protected void MediaDataSource_InsertObject(object sender, Csla.Web.InsertObjectArgs e)
    {
        Media obj = GetMedia();
        Csla.Data.DataMapper.Map(e.Values, obj, true);
        DropDownList ddlRegions = MediaDetailsView.FindControl("ddlRegions") as DropDownList;
        obj.RegionId = int.Parse(ddlRegions.SelectedValue);
        DropDownList ddlMediaTypes = MediaDetailsView.FindControl("ddlMediaTypes") as DropDownList;
        obj.Category = (MediaType)int.Parse(ddlMediaTypes.SelectedValue);
        e.RowsAffected = SaveMedia(obj);
    }

    protected void MediaDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = GetMedia();
    }

    protected void MediaDataSource_UpdateObject(object sender, Csla.Web.UpdateObjectArgs e)
    {
        Media obj = GetMedia();
        Csla.Data.DataMapper.Map(e.Values, obj, true);
        DropDownList ddlRegions = MediaDetailsView.FindControl("ddlRegions") as DropDownList;
        obj.RegionId = int.Parse(ddlRegions.SelectedValue);
        DropDownList ddlMediaTypes = MediaDetailsView.FindControl("ddlMediaTypes") as DropDownList;
        obj.Category = (MediaType)int.Parse(ddlMediaTypes.SelectedValue);
        e.RowsAffected = SaveMedia(obj);
    }

    #endregion

    #region MediaDetailsView

    protected void MediaDetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        Media obj = GetMedia();
        if (!obj.IsNew)
        {
            Response.Redirect("~/Media/Media.aspx?id=" + obj.Id.ToString(), true);
        }
    }

    protected void MediaDetailsView_DataBound(object sender, EventArgs e)
    {
        if (MediaDetailsView.CurrentMode != DetailsViewMode.ReadOnly)
        {
            if (this.MediaDetailsView.DataItem != null)
            {
                Media media = this.MediaDetailsView.DataItem as Media;
                DropDownList ddlRegions = MediaDetailsView.FindControl("ddlRegions") as DropDownList;
                ddlRegions.SelectedValue = media.RegionId.ToString();

                DropDownList ddlMediaTypes = MediaDetailsView.FindControl("ddlMediaTypes") as DropDownList;
                ddlMediaTypes.SelectedValue = ((int)media.Category).ToString();
            }
        }
    }

    protected void MediaDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        Label error = MediaDetailsView.FindControl("lblNameError") as Label;
        if (Media.CheckNameIsUsed(e.Values["Name"].ToString()))
        {
            error.Text = "媒介名称已存在，请重新输入。";
            e.Cancel = true;
        }
        else
        {
            error.Text = "";
        }
    }
    protected void MediaDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        Label error = MediaDetailsView.FindControl("lblNameError") as Label;
        if (e.OldValues["Name"].ToString() != e.NewValues["Name"].ToString()
            && Media.CheckNameIsUsed(e.NewValues["Name"].ToString()))
        {
            error.Text = "媒介名称已存在，请重新输入。";
            e.Cancel = true;
        }
        else
        {
            error.Text = "";
        }
    }

    #endregion

    
}
